/************************************************************************/
/* Resources                                                            */
/************************************************************************/
#include "Resources/sfx/SDLSound.h"

namespace TD_Resources {

	bool SDLSound::m_mixerInitialized = false;
	

	SDLSound::SDLSound() : Resource() {
		m_pSound = NULL;
	}

	SDLSound::SDLSound(const SDLSound &rhs) : Resource(rhs) {

	}

	SDLSound & SDLSound::operator=(const SDLSound &rhs) {
		Resource::operator =(rhs);
		return(*this);
	}

	SDLSound::SDLSound(const string &musicName) : Resource(musicName){
		m_pSound = NULL;
	}

	SDLSound::~SDLSound() {
		if(m_pSound != NULL) {
			if(m_soundChannel > 0)
				Mix_HaltChannel(m_soundChannel);
			
			Mix_FreeChunk(m_pSound);
			m_pSound = NULL;
		}
	}

	bool SDLSound::loadFile(const string &file) {

		/* Loads up the sound */
		m_pSound = Mix_LoadWAV(file.c_str());
		if(m_pSound == NULL) {
			initSoundMixer();
			m_pSound = Mix_LoadWAV(file.c_str());
			if(m_pSound == NULL)
				return false;

		}
		return true;

	}

	
	void SDLSound::playSound(bool loopMusic) {
		if(m_pSound != NULL) {
			if(loopMusic) {
				m_soundChannel = Mix_PlayChannel(-1, m_pSound, -1);
			} else {
				m_soundChannel = Mix_PlayChannel(-1, m_pSound, 0);
			}
		} 
		/*if(m_name == "SFX/sounds/snifitShot.wav" || m_name == "SFX/sounds/snifitHit.wav" || "SFX/sounds/Pause.wav") {
			Mix_Volume(m_soundChannel, MIX_MAX_VOLUME*0.60);
		} else if(m_name == "SFX/sounds/shyguyShot.wav") {
			Mix_Volume(m_soundChannel, MIX_MAX_VOLUME*0.55); 
		} else if(m_name == "SFX/sounds/magikoopaShot.wav" || m_name == "SFX/sounds/jump.wav" || "SFX/sounds/bosshahaha.wav") {
			Mix_Volume(m_soundChannel, MIX_MAX_VOLUME);
		} else {
			Mix_Volume(m_soundChannel, MIX_MAX_VOLUME*3/4);
		}*/
      Mix_Volume(m_soundChannel, MIX_MAX_VOLUME);
	}

	void SDLSound::initSoundMixer() {

#ifdef _WII
		int audio_rate = 32000;
		Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
		int audio_channels = 2;
		int audio_buffers = 512;
#elif WIN32
		int audio_rate = 44100;
		Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
		int audio_channels = 2;
		int audio_buffers = 4096;
#endif



		if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) ){
			m_mixerInitialized = false;
			exit(1);
		}

		m_mixerInitialized = true;
		

	}

	void SDLSound::render(SDL_Surface *g_destSurface, unsigned int deltaTime) {

	}


}